import pygame
import os
import random

# 初始化 Pygame
pygame.init()

# 设置游戏窗口
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Auto Player")

# 获取 img 文件夹下的所有图片文件
script_dir = os.path.dirname(os.path.abspath(__file__))
img_folder = os.path.join(script_dir, "img")

image_files = [os.path.join(img_folder, f) for f in os.listdir(img_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]

if not image_files:
    print("img 文件夹下没有图片文件，请检查。")
    pygame.quit()
    quit()

# 随机选择一张图片显示
current_image = pygame.image.load(random.choice(image_files))
current_image = pygame.transform.scale(current_image, (screen_width, screen_height))

# 初始化变量
paused = False
switch_event = pygame.USEREVENT + 1
pygame.time.set_timer(switch_event, 1500)  # 每 1.5 秒触发一次切换事件

# 游戏主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 点击鼠标时暂停或继续切换
            paused = not paused
            if paused:
                pygame.time.set_timer(switch_event, 0)  # 停止定时器
            else:
                pygame.time.set_timer(switch_event, 1500)  # 重新启动定时器
        elif event.type == switch_event and not paused:
            # 切换图片
            new_image_path = random.choice(image_files)
            current_image = pygame.image.load(new_image_path)
            current_image = pygame.transform.scale(current_image, (screen_width, screen_height))

    # 绘制背景和当前图片
    screen.fill((255, 255, 255))
    screen.blit(current_image, (0, 0))

    # 更新显示
    pygame.display.flip()

# 退出 Pygame
pygame.quit()